/*
 *  $TEAM_NAME Space Trader
 */
package views;

import models.Health;
import models.Player;
import models.Shipyard;
import models.Ship;
import models.FuelTank;

import java.text.NumberFormat;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.imageio.ImageIO;
import javax.swing.border.LineBorder;

import java.awt.event.ActionListener;
import java.awt.Font;
import java.awt.Image;
import java.awt.Dimension;
import java.awt.Color;

import java.io.File;

/*
 * Represents an individual row in the ship yard table
 * 
 * @author Jason Malia
 */

/**
 * Ship Panel
 * @author twb
 * @version 1.0
 */
public class ShipPanel extends JPanel {
	/**
	 * field
	 */
	private Ship ship;
    
	/**
	 * field
	 */
	private FuelTank fuel;
    
	/**
	 * field
	 */
	private Health health;
    
	/**
	 * field
	 */
	private final Player player;

	/**
	 * field
	 */
	private JLabel shipIcon;
    
	/**
	 * field
	 */
	private final JLabel shipNameField;
    
    /**
	 * field
	 */
	private final JLabel priceField;
    
	/**
	 * field
	 */
	private final JButton buyButton;
	
	/**
	 * field
	 */
	private static final int THIRTY_FIVE = 35;
	
	/**
	 * field
	 */
	private static final int TEN = 10;
	
	/**
	 * field
	 */
    private static final int TWELVE = 12;
    
    /**
     * field
     */
	private static final int TWO = 2;
	
	/**
	 * 
	 */
    private static final int FIFTEEN = 15;
    
    /**
     * 
     */
    private static final int THREE = 3;
	
    /**
     * Constructs a row of the ship yard table
     * 
     * @param player The game's player
     * @param ship The ship being sold
     */
    public ShipPanel(Player player, Ship ship) {
        super.setLayout(null);     
        super.setBorder(new LineBorder(Color.BLACK));
        super.setBackground(Color.WHITE);

        this.player = player;
        this.ship = ship;
        this.fuel = player.getShip().getFuelTank();

        try {
        	final File imageFile = new File("resources/" + ship.getName() + ".jpg");
        	
            final Image shipImage = ImageIO.read(imageFile).getScaledInstance(
            		THIRTY_FIVE, THIRTY_FIVE, Image.SCALE_DEFAULT);
            shipIcon = new JLabel(new ImageIcon(shipImage));
            shipIcon.setSize(TEN, TEN);
        } catch (Exception e) {
			System.err.println(e);

            shipIcon = new JLabel(); 
        }

        //shipIcon.setBorder(new LineBorder(Color.BLACK));
        shipNameField = new JLabel("  " + ship.getName());

        buyButton = new JButton("Buy");
        
        priceField = new JLabel();

        super.add(shipIcon);
        super.add(shipNameField);
        super.add(buyButton);
        super.add(priceField);

        update();
    }
    
    /**
     * Constructs a row of the ship yard table to be used for refueling
     * 
     * @param player The game's player
     * @param health
     */
    public ShipPanel(Player player, Health health) {
        super.setLayout(null);     
        super.setBorder(new LineBorder(Color.BLACK));
        super.setBackground(Color.WHITE);

        this.player = player;
        this.health = health;

        try {
        	final File imageFile = new File("resources/repair.jpg");
            final Image fuelImage = ImageIO.read(imageFile).getScaledInstance(
            		THIRTY_FIVE, THIRTY_FIVE, Image.SCALE_DEFAULT);
            shipIcon = new JLabel(new ImageIcon(fuelImage));
            shipIcon.setSize(TEN, TEN);
        } catch (Exception e) {
			System.err.println(e);

            shipIcon = new JLabel(); 
        }

        shipNameField = new JLabel("  Repair Ship");

        buyButton = new JButton("Buy");
        
        priceField = new JLabel();

        super.add(shipIcon);
        super.add(shipNameField);
        super.add(buyButton);
        super.add(priceField);

        update();
    }

    /**
     * ship panel
     * @param player
     */
    public ShipPanel(Player player) {
    	super.setLayout(null);     
    	super.setBorder(new LineBorder(Color.BLACK));
    	super.setBackground(Color.WHITE);

        this.player = player;

        try {
        	final File imageFile = new File("resources/fuel.png");
    		final Image fuelImage = ImageIO.read(imageFile).getScaledInstance(
    				THIRTY_FIVE, THIRTY_FIVE, Image.SCALE_DEFAULT);
    		shipIcon = new JLabel(new ImageIcon(fuelImage));
            shipIcon.setSize(TEN, TEN);
        } catch (Exception e) {
			System.err.println(e);

            shipIcon = new JLabel(); 
        }

        shipNameField = new JLabel("  REFUEL");

        buyButton = new JButton("Buy");
        
        priceField = new JLabel();

        super.add(shipIcon);
        super.add(shipNameField);
        super.add(buyButton);
        super.add(priceField);

        update();
    }
    
    /**
     * Updates a row of the ship yard table based on game logic and current system
     * 
     */

    public final void update() {
        final Shipyard shipyard = player.getLocation().getShipyard();
        
        final NumberFormat fmt = NumberFormat.getCurrencyInstance();
        final Font tooMuch = new Font("TooMuch", Font.ITALIC, TWELVE);
        final Font enough = new Font("Enough", Font.BOLD, TWELVE);
        
        if (ship != null){
	        priceField.setText(fmt.format(shipyard.getPrice(ship)));
	        if (shipyard.getPrice(ship) > player.getCredits()){
	        	priceField.setFont(tooMuch);
	        	priceField.setForeground(Color.RED);
	        	buyButton.setEnabled(false);
	        }
	        else {
	        	priceField.setFont(enough);
	        	priceField.setForeground(Color.DARK_GRAY);
	        	buyButton.setEnabled(true);
	        }
	        
	        if (ship.getMinTechLevel().ordinal() 
	        		> player.getLocation().getTechLevel().ordinal()){
	        	buyButton.setEnabled(false);
	        }
	        else if(ship.getName().equals(player.getShip().getName())){
	        	buyButton.setEnabled(false);
	        	priceField.setText("Owned");
	        	priceField.setFont(tooMuch);
	        	priceField.setForeground(Color.DARK_GRAY);
	        }
	        else {
	        	priceField.setText(fmt.format((ship.getPrice())));
	        }
        }
        else if (health != null){
        	health = player.getShip().getHealth();
        	final int currentHealth = health.getValue();
        	final int maxHealth = health.getMaximum();
        	priceField.setText(fmt.format((maxHealth - currentHealth) * 1));
        }
        else {
        	fuel = player.getShip().getFuel();
        	priceField.setText(fmt.format(
        			(fuel.getMaxFuel() 
        					- fuel.getCurrentFuel()) 
        					* player.getShip().getFUEL_COST()));
        }
        	
    }
    
    /**
     * Lays out the elements of the row (e.g Buy Button etc.)
     */

    public void layoutSubviews() {
        final Dimension size = getSize();

        int x = 0;

        //shipIcon.setBorder(new LineBorder(Color.BLACK));
        shipIcon.setBounds(0, 0, size.height, size.height);
        x += size.height;

        shipNameField.setBounds(x, 0, (size.height << TWO), size.height); 
        x += size.height * (THREE);

        priceField.setBounds(x, 0, (size.height << TWO), size.height);
        x += size.height;

        x = size.width;

        buyButton.setBounds(x - size.height - FIFTEEN, 0, 
        		size.height + FIFTEEN, size.height);
        x -= size.height + (TEN);
    }

    /**
     * Get's the ship (fuel) being sold
     * @return ship
     */
    public Ship getShip() {
        return ship;
    }

    /**
     * Add an actionlistener to the buy button
     * @param listener
     */
    public void addActionListener(ActionListener listener) {
        buyButton.addActionListener(listener);
    }

    /**
     * Sets the bounds of sub units of the row
     * 
     * @param x X-Position
     * @param y Y-Position
     * @param width Width of the sub unit
     * @param height Height of the sub unit
     */
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        layoutSubviews();
    }

	/**
	 * @return the health
	 */
	public Health getHealth() {
		return health;
	}

	/**
	 * @param health the health to set
	 */
	public void setHealth(Health health) {
		this.health = health;
	}
}
